package com.sl.au.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sl.au.entity.Bid;
import com.sl.au.entity.BidRecord;
import com.sl.au.repository.AuditNodeRepository;
import com.sl.au.repository.BidRecordRepository;
import com.sl.au.repository.BidRepository;
import com.sl.au.service.AuditNodeService;
import com.sl.au.service.BidService;

@Service("bidService")
@Transactional
public class BidServiceImpl extends BaseServiceImpl implements BidService {

	@Autowired
	BidRepository bidRepository;
	
	@Autowired
	BidRecordRepository bidRecordRepository;
	
	@Autowired
	AuditNodeRepository auditNodeRepository;

	@Autowired
	AuditNodeService auditNodeService;
	public BidRepository getRepository() {
		return this.bidRepository;
	}

	@Override
	public void saveProcessStart(String bidId, ProcessInstance pInst) {
		Bid bid = bidRepository.getOne(bidId);
		if (null == pInst || null == bid)
			return;
		// 如果是重新提交审批，删除原来的审批节点
		auditNodeService.getRepository().deleteByPInstId(bid.getProcessInstId());
		bid.setProcessInstId(pInst.getId());
		bid.setAuditState("已提交");
		bid.setStartDate(new Date());	

	}

	@Override
	public List<Bid> getBid(int start, int length, Map<String,String> condition ) {
		Page<Bid> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		if (condition.get("dataFilter").equals("所有"))
			if(condition.get("auditState").equals("未审批")){
				pages =bidRepository.findBid(condition.get("key"),pageable);
			}else if(condition.get("auditState").equals("全部")){
				pages = bidRepository.findBidAll(condition.get("key"),pageable);
			}else{
				pages =bidRepository.findBid(condition.get("key"),condition.get("auditState"), pageable);
			}
		else if (condition.get("dataFilter").equals("本部门"))
			if(condition.get("auditState").equals("未审批")){
				pages = bidRepository.findBid(condition.get("key"), condition.get("superior"), condition.get("userId"),pageable);
			}else if(condition.get("auditState").equals("全部")){
				pages = bidRepository.findBidAll(condition.get("key"), condition.get("superior"), condition.get("userId"),pageable);
			}else{
				pages = bidRepository.findBid(condition.get("key"), condition.get("superior"), condition.get("userId"),condition.get("auditState"),pageable);
			}
		else if (condition.get("dataFilter").equals("本片区")){
			if(condition.get("auditState").equals("未审批")){
				pages = bidRepository.findAreaBid(condition.get("key"), condition.get("areaId"), condition.get("userId"),pageable);
			}else if(condition.get("auditState").equals("全部")){
				pages = bidRepository.findAreaBidAll(condition.get("key"), condition.get("areaId"), condition.get("userId"),pageable);
			}else{
				pages = bidRepository.findAreaBid(condition.get("key"), condition.get("areaId"), condition.get("userId"),condition.get("auditState"),pageable);
			}
		}
		else if(condition.get("dataFilter").equals("本人")){
			if(condition.get("auditState").equals("未审批")){
				pages = bidRepository.findSelfBid(condition.get("key"), condition.get("userId"), pageable);
			}else if(condition.get("auditState").equals("全部")){
				pages = bidRepository.findSelfBidAll(condition.get("key"), condition.get("userId"), pageable);
			}else{
				pages = bidRepository.findSelfBid(condition.get("key"), condition.get("userId"),condition.get("auditState"), pageable);
			}
		}
		return pages.getContent();
	}
	
	@Override
	public long getBidCount(Map<String, String> condition) {
		long count = 0;
		if (condition.get("dataFilter").equals("所有")){
			if(condition.get("auditState").equals("未审批")){
				count = bidRepository.getCount(condition.get("key"));
			}else if(condition.get("auditState").equals("全部")){
				count = bidRepository.getCountAll(condition.get("key"));
			}else{
				count = bidRepository.getCount(condition.get("key"),condition.get("auditState"));
			}
		}
		else if (condition.get("dataFilter").equals("本部门")){
			if(condition.get("auditState").equals("未审批")){
				count = bidRepository.getCount(condition.get("key"), condition.get("superior"),condition.get("userId"));
			}else if(condition.get("auditState").equals("全部")){
				count = bidRepository.getCountAll(condition.get("key"), condition.get("superior"),condition.get("userId"));
			}else{
				count = bidRepository.getCount(condition.get("key"), condition.get("superior"),condition.get("userId"),condition.get("auditState"));
			}
			//count = bidRepository.getCount(condition.get("key"), condition.get("superior"),condition.get("userId"));
		}
		else if (condition.get("dataFilter").equals("本片区")){
			if(condition.get("auditState").equals("未审批")){
				count = bidRepository.getAreaCount(condition.get("key"), condition.get("areaId"),condition.get("userId"));
			}else if(condition.get("auditState").equals("全部")){
				count = bidRepository.getAreaCountAll(condition.get("key"), condition.get("areaId"),condition.get("userId"));
			}else{
				count = bidRepository.getAreaCount(condition.get("key"), condition.get("areaId"),condition.get("userId"),condition.get("auditState"));
			}
		}
		else if (condition.get("dataFilter").equals("本人")){
			if(condition.get("auditState").equals("未审批")){
				count = bidRepository.getSelfCount(condition.get("key"), condition.get("userId"));
			}else if(condition.get("auditState").equals("全部")){
				count = bidRepository.getSelfCountAll(condition.get("key"), condition.get("userId"));
			}else{
				count = bidRepository.getSelfCount(condition.get("key"), condition.get("userId"),condition.get("auditState"));
			}
		}
			//count = bidRepository.getSelfCount(condition.get("key"), condition.get("userId"));
		return count;
	}

	@Override
	public List<BidRecord> getBidRecord(int start, int length, String bidId) {
		Page<BidRecord> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
        pages =bidRecordRepository.findBidRecord(bidId, pageable);
		return pages.getContent();
	}

	

}
